﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;
using Castle.Windsor;

public partial class Education_CauseManage : ContainerBasePage
{

    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void btnBind_Click(object sender, EventArgs e)
    {
        Bind(1);
    }

    private void Bind(int pageIndex)
    {
        //获取查询条件类型和值
        int qType = int.Parse(HFQueryType.Value);
        string qValue = HFQueryValue.Value;

        IGlobalCauseInfoLogic causeLogic = Container<IGlobalCauseInfoLogic>.Instance;
        List<GlobalCauseInfo> list = null;
        Hashtable table = new Hashtable();

        switch (qType)
        {
            //0: 查询所有
            case 0:
                //根据条件查询数据库
                table.Add("IsEffect", 1);
                list = (List<GlobalCauseInfo>)causeLogic.FindAll(table);

                //分页处理
                PagerBind(list, pageIndex);

                break;
            //1: 按学区编号查询
            case 1:
                //根据条件查询数据库
                table.Add("CauseNo", qValue);
                table.Add("IsEffect", 1);
                list = (List<GlobalCauseInfo>)causeLogic.FindAll(table);

                //分页处理
                PagerBind(list, pageIndex);

                break;
            //2: 按学区名称查询
            case 2:
                //根据条件查询数据库
                table.Add("CauseName", qValue);
                table.Add("IsEffect", 1);
                list = (List<GlobalCauseInfo>)causeLogic.FindAll(table);

                //分页处理
                PagerBind(list, pageIndex);

                break;
            default:
                break;
        }
    }

    //分页处理
    private void PagerBind(List<GlobalCauseInfo> srcList, int pageIndex)
    {
        ICouseGradeInfoLogic couseGradeLogic = Container<ICouseGradeInfoLogic>.Instance;

        AspNetPager1.RecordCount = srcList.Count;
        AspNetPager1.CurrentPageIndex = pageIndex;

        int index = (pageIndex - 1) * AspNetPager1.PageSize;
        int count = 0;
        if (srcList.Count - index >= AspNetPager1.PageSize) count = AspNetPager1.PageSize;
        else count = srcList.Count - index;

        List<GlobalCauseInfo> pageList = srcList.GetRange(index, count);
        List<CourseView> viewList = new List<CourseView>();

        foreach (GlobalCauseInfo globalCauseInfo in pageList)
        {
            Hashtable ht = new Hashtable();
            ht.Add("CourseID", globalCauseInfo.RID);

            CourseView gv = new CourseView();
            gv.RID = globalCauseInfo.RID;
            gv.CauseNo = globalCauseInfo.CauseNo;
            gv.CauseName = globalCauseInfo.CauseName;
            gv.IsEffect = globalCauseInfo.IsEffect;
            gv.GradeList = new List<int>();

            viewList.Add(gv);
            StringBuilder sb = new StringBuilder();
            IList<CouseGradeInfo> cgList = couseGradeLogic.FindAll(ht);
            foreach (CouseGradeInfo couseGradeInfo in cgList)
            {
             
                gv.GradeList.Add(couseGradeInfo.Grade);
            }
            foreach (int i in gv.GradeList)
            {
                sb.Append(i.ToString() + ",");
            }
            gv.GradeString = ConvertGradeString(gv.GradeList);
            gv.GradeIntString = sb.ToString();
        }

        Repeater1.DataSource = viewList;
        Repeater1.DataBind();
    }

    private string ConvertGradeString(IList<int> list)
    {
        StringBuilder sb = new StringBuilder("");
        foreach (int i in list)
        { 
            switch(i)
            {
                case 7:
                    sb.Append("初一 ");
                    break;
                    
                case 8:
                    sb.Append("初二 ");
                    break;
                    
                case 9:
                    sb.Append("初三 ");
                    break;
            }
        }
        return sb.ToString();
    }

    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        Wuqi.Webdiyer.PageChangedEventArgs pc = e as Wuqi.Webdiyer.PageChangedEventArgs;

        Bind(pc.NewPageIndex);
    }

    //////////////////////////////////////////////////////////////////////////


    //根据课程编号来绑定数据源
    //[System.Web.Services.WebMethod]
    //public static int BindDataByCauseNO(string strCauseNo)
    //{
    //    if (String.IsNullOrEmpty(strCauseNo))
    //        return -1;

    //    int causeNo = int.Parse(strCauseNo);

    //    Hashtable table = new Hashtable();
    //    table.Add("CauseNo", causeNo);
    //    table.Add("IsEffect", 1);

    //    IGlobalCauseInfoLogic causeLogic = Container<IGlobalCauseInfoLogic>.Instance;
    //    List<GlobalCauseInfo> list = (List<GlobalCauseInfo>)causeLogic.FindAll(table);

    //    causeView = list;

    //    return 0;
    //}

    ////根据课程名称来绑定数据源
    //[System.Web.Services.WebMethod]
    //public static int BindDataByCauseName(string strCauseName)
    //{
    //    if (String.IsNullOrEmpty(strCauseName))
    //        return -1;


    //    Hashtable table = new Hashtable();
    //    table.Add("CauseName", strCauseName);
    //    table.Add("IsEffect", 1);

    //    IGlobalCauseInfoLogic causeLogic = Container<IGlobalCauseInfoLogic>.Instance;
    //    List<GlobalCauseInfo> list = (List<GlobalCauseInfo>)causeLogic.FindAll(table);

    //    causeView = list;

    //    return 0;
    //}
    //根据课程ID来查询课程信息
    [System.Web.Services.WebMethod]
    public static GlobalCauseInfo SelectCause(int causeId)
    {
        IGlobalCauseInfoLogic causeLogic = Container<IGlobalCauseInfoLogic>.Instance;
        GlobalCauseInfo causeInfo = (GlobalCauseInfo)causeLogic.FindById(causeId);
        return causeInfo;
    }
    //根据编号来查询课程信息
    [System.Web.Services.WebMethod]
    public static int SelectCountByNo(int causeNo)
    {
        Hashtable table = new Hashtable();
        table.Add("CauseNo", causeNo);
        table.Add("IsEffect", 1);

        int n = Container<IGlobalCauseInfoLogic>.Instance.FindCount(table);
        return n;
    }
    //根据课程名称，查询课程信息
    [System.Web.Services.WebMethod]
    public static int SelectCountByName(string causeName)
    {
        Hashtable table = new Hashtable();
        table.Add("CauseName", causeName);
        table.Add("IsEffect", 1);

        int n = Container<IGlobalCauseInfoLogic>.Instance.FindCount(table);
        return n;
    }

    //验证课程ID、课程名称是否使用,除了他本身以外
    [System.Web.Services.WebMethod]
    public static int ValidateUsed(GlobalCauseInfo causeInfo)
    {

        int result = 0;
        IGlobalCauseInfoLogic causeLogic = Container<IGlobalCauseInfoLogic>.Instance;

        //1.新增
        if (causeInfo.RID == 0)
        {
            Hashtable tableName = new Hashtable();
            tableName.Add("CauseName", causeInfo.CauseName);
            tableName.Add("IsEffect", 1);
            result = causeLogic.FindCount(tableName);
            //课程名称重复
            if (result > 0) 
                return 1;       //在有效数据里面已经存在这个课程编号，则返回2

            if(tableName.ContainsKey("IsEffect"))
                tableName["IsEffect"] = 0;

            result = causeLogic.FindCount(tableName);
            //课程名称重复
            if (result > 0)
                return 2;       //在无效数据里面已经存在这个课程编号，则返回2            
        }
        //2.修改
        else
        {
            GlobalCauseInfo gc = causeLogic.FindById(causeInfo.RID);

            //修改了课程名称
            if (gc.CauseName.ToUpper() != causeInfo.CauseName.ToUpper())
            {
                Hashtable tableName = new Hashtable();
                tableName.Add("CauseName", causeInfo.CauseName);
                tableName.Add("IsEffect", 1);
                result = causeLogic.FindCount(tableName);
                //课程名称重复
                if (result > 0)
                    return 1;

                if (tableName.ContainsKey("IsEffect"))
                    tableName["IsEffect"] = 0;

                result = causeLogic.FindCount(tableName);
                //课程名称重复
                if (result > 0)
                    return 2;       //在无效数据里面已经存在这个课程编号，则返回2      
            }
        }

        return result;
    }

    //插入一个课程信息
    [System.Web.Services.WebMethod]
    public static int InsertCauseInfo(GlobalCauseInfo causeInfo, List<String> gradeList)
    {
        causeInfo.IsEffect = 1;
        int newrid = Container<IGlobalCauseInfoLogic>.Instance.InsertNewCause(causeInfo, SchoolMark);
        if(Common.CurrentVersion == EduVersion.School)
        {
            try
            {
                CauseInfo ci = new CauseInfo(newrid, Common.GetConstSchoolMark());
                Container<ICauseInfoLogic>.Instance.Create(ci);
            }
            catch
            { }
        }
        foreach (string s in gradeList)
        {
            CouseGradeInfo cg = new CouseGradeInfo();
            cg.CourseID = newrid;
            cg.Grade = Int32.Parse(s);
            cg.SchoolMark = Common.GetConstSchoolMark();
            Container<ICouseGradeInfoLogic>.Instance.Create(cg);
        }
        return newrid;
    }
    //更新一个课程信息
    [System.Web.Services.WebMethod]
    public static int UpdateCauseInfo(GlobalCauseInfo causeInfo, List<String> gradeList)
    {
        causeInfo.IsEffect = 1;
        int iResult = Container<IGlobalCauseInfoLogic>.Instance.UpdateCauseName(causeInfo);

        Hashtable ht = new Hashtable();
        ht.Add("CourseID", causeInfo.RID);
        Container<ICouseGradeInfoLogic>.Instance.DeleteBy(ht);
        foreach (string s in gradeList)
        {
            CouseGradeInfo cg = new CouseGradeInfo();
            cg.CourseID = causeInfo.RID;
            cg.Grade = Int32.Parse(s);
            cg.SchoolMark = Common.GetConstSchoolMark();
            Container<ICouseGradeInfoLogic>.Instance.Create(cg);
        }
        return iResult;
    }
    //删除一个课程信息
    [System.Web.Services.WebMethod]
    public static int DeleteCauseInfo(int causeID)
    {
        if (Common.CurrentVersion == EduVersion.School)
        {
            int iCount = Container<ICauseInfoLogic>.Instance.FindCauseUserCount(causeID, Common.GetConstSchoolMark());
            if (iCount > 0)
                return -1;
            Container<ICauseInfoLogic>.Instance.DeleteById(causeID, Common.GetConstSchoolMark());
            Container<IGlobalCauseInfoLogic>.Instance.Delete(causeID);

            Hashtable ht = new Hashtable();
            ht.Add("CourseID", causeID);
            Container<ICouseGradeInfoLogic>.Instance.DeleteBy(ht);

            return 1;
        }
        int n = Container<IGlobalCauseInfoLogic>.Instance.UpdateIsEffect(0,causeID);

        return n;
    }

    //查询这门课程的使用次数
    [System.Web.Services.WebMethod]
    public static int GetUseCountByCauseID(int causeID)
    {
        Hashtable table = new Hashtable();
        table.Add("RID", causeID);
        if(Common.CurrentVersion != EduVersion.School)
        {
            int n = Container<ICauseInfoLogic>.Instance.FindCount(table);
            return n;
        }
        return 0;
    }

    /// <summary>
    /// 根据状态上移或者下移课程编号
    /// </summary>
    /// <param name="iCauseNo"></param>
    /// <param name="iState"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int MoveCauseNO(int iCauseRid, int iState)
    {
        /*
        //return Container<IGlobalCauseInfoLogic>.Instance.MoveCauseNO(iCauseNo, iState);

        //返回-1：到达尾部
        //返回-2：到达顶部
        //返回-3：调整失败

        GlobalCauseInfo srcGlobalCause = null;
        GlobalCauseInfo desGlobalCause = null;

        //上移，找到上面一个结点信息
        if (iState == -1)
        {
            foreach (GlobalCauseInfo item in causeView)
            {
                if (item.RID == iCauseRid)
                {
                    srcGlobalCause = item;
                    break;
                }
                desGlobalCause = item;
            }

            //没找到源
            if (srcGlobalCause == null) return -3;

            //到达顶点项
            if (desGlobalCause == null) return -2;

            //开始交换两个结点的课程编号，不能交换课程名
            int tempCauseNo = desGlobalCause.CauseNo;
            desGlobalCause.CauseNo = srcGlobalCause.CauseNo;
            srcGlobalCause.CauseNo = tempCauseNo;

            int iResult = Container<IGlobalCauseInfoLogic>.Instance.ExchangeCauseNO(srcGlobalCause, desGlobalCause);

            return iResult;
        }

        //下移，找到下面一个结点信息
        if (iState == 1)
        {
            foreach (GlobalCauseInfo item in causeView)
            {
                if (item.RID == iCauseRid && srcGlobalCause == null)
                {
                    srcGlobalCause = item;
                    continue;
                }

                if (srcGlobalCause != null)
                {
                    desGlobalCause = item;
                    break;
                }
            }

            //没找到源
            if (srcGlobalCause == null) return -3;

            //到达尾部
            if (desGlobalCause == null) return -1;

            //开始交换两个结点的课程编号，不能交换课程名
            int tempCauseNo = desGlobalCause.CauseNo;
            desGlobalCause.CauseNo = srcGlobalCause.CauseNo;
            srcGlobalCause.CauseNo = tempCauseNo;

            int iResult = Container<IGlobalCauseInfoLogic>.Instance.ExchangeCauseNO(srcGlobalCause, desGlobalCause);

            return iResult;
        }
        
        return 1;*/

        int iResult = 0;

        if (iState == -1)       //上移，找到上面一个结点信息
        {
            iResult = Container<IGlobalCauseInfoLogic>.Instance.ExchangeCuaseNo(2, iCauseRid);
        }   
        else if (iState == 1)   //下移，找到下面一个结点信息
        {
            iResult = Container<IGlobalCauseInfoLogic>.Instance.ExchangeCuaseNo(1, iCauseRid);
        }

        return iResult;
    }




}
